package com.qq5194102.simpleskills.skill.sword;

import com.qq5194102.simpleskills.core.PlayerDataManager;
import com.qq5194102.simpleskills.model.ExPlayerData;
import com.qq5194102.simpleskills.model.SwordPlayerData;
import com.qq5194102.simpleskills.util.EffectUtil;
import com.qq5194102.simpleskills.util.SwordUtil;
import org.bukkit.*;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.FireworkMeta;

public class SwordListener implements Listener {
    public SwordListener() {
    }


    /**
     * 监听玩家攻击事件 被动技能暴击
     * @param event
     */
    @EventHandler
    public void hit(EntityDamageByEntityEvent event){
        // 前置校验
        Entity damager = event.getDamager();
        if (!(damager instanceof Player)) {
            return;
        }
        Player player = (Player) damager;

        // 持剑校验
        if (!SwordUtil.playerMainHandIsSword(player)) {
            return;
        }

        // 被击生物校验
        Entity victim = event.getEntity();
        if (!(victim instanceof org.bukkit.entity.LivingEntity)) {
            return;
        }

        // 被动技能1：暴击伤害处理
        double damage = event.getFinalDamage();
        double handledDamage = PlayerDataManager.getPlayerData(player.getUniqueId()).getSwordSkillData().getHandledDamage(damage);
        if (handledDamage>damage){
            event.setDamage(handledDamage);
            // 可选：添加视觉特效
            if (victim.getWorld() != null) {
                victim.getWorld().playEffect(victim.getLocation(),
                        org.bukkit.Effect.STEP_SOUND,
                        org.bukkit.Material.REDSTONE_BLOCK);
            }
            // 消息提示
            player.sendMessage("§c暴击！ -"+ (int) handledDamage);
        }


    }
    /**
     * 监听击杀事件
     * 实体死亡后 判断1、是否玩家击杀 2、是否剑击杀 3、是否有经验生物
     * 击杀生物有经验则加经验 判断技能是否升级
     * @param event 实体死亡事件
     */
    @EventHandler
    public void onMobKill(EntityDeathEvent event) {
        LivingEntity entity = event.getEntity();
        Player player = entity.getKiller();

        // 检查是否由玩家击杀
        if (player == null) {
            return;
        }

        if (!SwordUtil.playerMainHandIsSword(player)) {
            return; // 不是用剑击杀，跳过
        }

        // 检查是否是有经验的生物（避免掉落物过多）
        if (event.getDroppedExp()<=0) {
            return;
        }


        // 加经验
        ExPlayerData playerData = PlayerDataManager.getPlayerData(player.getUniqueId());
        SwordPlayerData swordSkillData = playerData.getSwordSkillData();

        int levelBefore = swordSkillData.getLevel();

        swordSkillData.addExperience(1);

        // 可选:添加击杀消息
        // player.sendMessage("§a你击杀了 §e" + getEntityName(entity) + " §a并获得了剑术经验！");


        // 判断是否升级 升级则放特效音效
        int levelAfter = swordSkillData.getLevel();
        if (levelAfter>levelBefore){
            EffectUtil.playEffect(player.getLocation(), EffectUtil.EffectType.RAINBOW);
            EffectUtil.playSound(player.getLocation());
            player.sendMessage("§c[技能系统] §a剑术等级提升 当前: §6"+levelAfter+"§a 级！");
        }
    }





    // todo 可以把下面这些封一个工具类等。。。

    /**
     * 获取实体名称
     * @param entity
     * @return
     */
    private String getEntityName(LivingEntity entity) {
        switch (entity.getType()) {
            case ZOMBIE: return "僵尸";
            case SKELETON: return "骷髅";
            case CREEPER: return "苦力怕";
            case SPIDER: return "蜘蛛";
            case ENDERMAN: return "末影人";
            case WITCH: return "女巫";
            case BLAZE: return "烈焰人";
            case WITHER_SKELETON: return "凋零骷髅";
            case ENDER_DRAGON: return "末影龙";
            case WITHER: return "凋灵";
            default: return entity.getType().name().toLowerCase();
        }
    }



}
